/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.cheln.explorer.ds.database;

import android.content.Context;
import android.database.Cursor;
import cn.cheln.explorer.beans.ApkInfo;
import cn.cheln.explorer.beans.ApkInfoUtil;
import cn.cheln.explorer.beans.FileInfo;
import cn.cheln.explorer.beans.FileUtil;
import cn.cheln.explorer.beans.MusicInfo;
import cn.cheln.support.log.Logs;
import cn.power.shine.R;

/**
 * 
 * @author chenliang
 */
public class DatasrcTransferer {

	public static <T extends FileInfo> T mediaTransf(Context context,
			Cursor cursor, Class<? extends FileInfo> clazz) {
		FileInfo fileInfo = null;
		try {
			if (cursor != null && cursor.isClosed()) {
				return null;
			}
			try {
				fileInfo = clazz.newInstance();

				fileInfo.setPath(cursor.getString(cursor
						.getColumnIndex("_data")));
//				fileInfo.setName(FileUtil.getName(fileInfo.getPath()));
//				fileInfo.setLeng(cursor.getLong(cursor.getColumnIndex("_size")));
//				fileInfo.setLastModified(DateFormat.format(
//						"yyyy-MM-dd kk:mm:ss",
//						new Date(cursor.getLong(cursor
//								.getColumnIndex("date_modified")) * 1000))
//						.toString());
//				fileInfo.setIconRes(-1);
				int flagIndex = cursor.getColumnIndex("flagorder");
				if (flagIndex != -1) {
					fileInfo.setForceDir(cursor.getString(flagIndex).equals(
							"-1"));
				}
//				fileInfo.setSizeText(FileUtil.formatSize(fileInfo.getLeng()));
//				fileInfo.setType(FileUtil.getRealExtension(fileInfo.getName()));
				fileInfo.buildFile();
				flagIndex = cursor.getColumnIndex("sort_key");
				if (flagIndex != -1) {
					fileInfo.sortKey = cursor.getString(flagIndex);
				}

			} catch (Exception ex) {
				return null;
			}
			if (clazz == MusicInfo.class) {
				preserMusicInfo(cursor, fileInfo, context);
			} else if (clazz == ApkInfo.class) {
                if (ApkInfoUtil.preserApkInfo(fileInfo, context)) {
                    return (T) fileInfo;
                }
            }
        } catch (Exception e) {
			e.printStackTrace();
		}
		return (T) fileInfo;
	}

	public static void preserMusicInfo(Cursor cursor, FileInfo fileInfo,
			Context context) {
		String album = cursor.getString(cursor.getColumnIndex("album"));
		((MusicInfo) fileInfo).album = album == null || album.equals("unknown") ? context
				.getString(R.string.music_unknown) : album;
		String tmp = FileUtil.getParent(fileInfo.getPath());
		tmp = tmp.substring(tmp.lastIndexOf("/") + 1);
		
		if (tmp.equals(((MusicInfo) fileInfo).album)) {
			((MusicInfo) fileInfo).album = context
					.getString(R.string.music_unknown);
		}
		((MusicInfo) fileInfo).album = "- " + ((MusicInfo) fileInfo).album;
		String artist = cursor.getString(cursor.getColumnIndex("artist"));
		((MusicInfo) fileInfo).artist = artist == null
				|| artist.equals("unknown") ? context
				.getString(R.string.music_unknown) : artist;
		((MusicInfo) fileInfo).thumbnail = cursor.getString(cursor
				.getColumnIndex("thumbnail"));
		Logs.i("thumb --------------------------" + ((MusicInfo) fileInfo).thumbnail
				);
		((MusicInfo) fileInfo).album_id = cursor.getInt(cursor
				.getColumnIndex("album_id"));
		((MusicInfo) fileInfo).album_artist_id = cursor.getInt(cursor
				.getColumnIndex("artist_id"));
	}
}
